package com.cction.activity.house

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import com.cction.R
import com.cction.comm.T
import com.cction.comm.Utils
import com.cction.entry.HouseBean
import com.flyco.tablayout.listener.OnTabSelectListener
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.house_main.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 房贷
 */
class HouseActivity : AppCompatActivity() {
    private val mTitles = arrayOf("商业房贷", "公积金房贷", "组合房贷")
    private val mPoor = arrayOf("8成", "7成", "6成", "5成", "4成", "3成", "2成")
    private var tabPosition = 0
    private var poor = 0.7f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.house_main)


        initView()
    }

    private fun initView() {
        title_bar_back.visibility = View.VISIBLE
        title_bar_back.setOnClickListener { finish() }

        title_bar_title.text = "房贷计算"
        title_bar_other.text = "设置"
        title_bar_other.visibility = View.GONE

        house_tab.setTabData(mTitles)

        house_tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                when (position) {
                    0 -> {
                        tabPosition = 0
                        house_s_layout.visibility = View.VISIBLE
                        house_g_layout.visibility = View.GONE
                        house_s_menoy_edit.isEnabled = false
                        val value = house_total_edit.text.toString()
                        if (!TextUtils.isEmpty(value)) {
                            val total = Utils.stringToDouble(value)
                            house_s_menoy_edit.setText("${Utils.keepPointDouble(total * poor)}")
                        }
                    }
                    1 -> {
                        tabPosition = 1
                        house_s_layout.visibility = View.GONE
                        house_g_layout.visibility = View.VISIBLE
                        house_g_menoy_edit.isEnabled = false
                        val value = house_total_edit.text.toString()
                        if (!TextUtils.isEmpty(value)) {
                            val total = Utils.stringToDouble(value)
                            house_g_menoy_edit.setText("${Utils.keepPointDouble(total * poor)}")
                        }
                    }
                    2 -> {
                        tabPosition = 2
                        house_s_layout.visibility = View.VISIBLE
                        house_g_layout.visibility = View.VISIBLE
                        house_s_menoy_edit.setText("")
                        house_g_menoy_edit.setText("")
                        house_s_menoy_edit.isEnabled = true
                        house_g_menoy_edit.isEnabled = true
                    }
                }

            }

            override fun onTabReselect(position: Int) {

            }
        })

        house_total_edit.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val value = s.toString()
                if (!TextUtils.isEmpty(value)) {
                    val total = Utils.stringToDouble(value)
                    if (tabPosition < 2) {
                        house_s_menoy_edit.setText("${Utils.keepPointDouble(total * poor)}")
                        house_g_menoy_edit.setText("${Utils.keepPointDouble(total * poor)}")
                    }
                }
            }
        })


        val adapter = ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, mPoor);
        house_poor_spinner.adapter = adapter
        house_poor_spinner.setSelection(1)
        house_poor_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                when (position) {
                    0 -> {
                        poor = 0.8f
                    }
                    1 -> {
                        poor = 0.7f
                    }
                    2 -> {
                        poor = 0.6f
                    }
                    3 -> {
                        poor = 0.5f
                    }
                    4 -> {
                        poor = 0.4f
                    }
                    5 -> {
                        poor = 0.3f
                    }
                    6 -> {
                        poor = 0.2f
                    }
                }
                val totalValue = house_total_edit.text.toString()
                if (!TextUtils.isEmpty(totalValue)) {
                    val total = Utils.stringToDouble(totalValue)
                    if (tabPosition < 2) {
                        house_s_menoy_edit.setText("${total * poor}")
                        house_g_menoy_edit.setText("${total * poor}")
                    }
                }
            }
        }

        house_calculate.setOnClickListener {
            if (checkData()) {
                val intent = Intent(this, HouseDetailActivity::class.java)
                val houseBean = HouseBean()
                houseBean.type = tabPosition + 1
                val data = house_s_time_edit.text.toString()
                houseBean.time = data.toInt()
                val total = house_total_edit.text.toString()
                houseBean.total = total.toDouble()

                houseBean.mortgage = poor

                val sLoan = house_s_menoy_edit.text.toString()
                val sRate = house_s_interest_edit.text.toString()

                val gLoan = house_g_menoy_edit.text.toString()
                val gRate = house_g_interest_edit.text.toString()
                when (tabPosition) {
                    0 -> {
                        houseBean.sLoan = sLoan.toDouble()
                        houseBean.sRate = sRate.toDouble()
                    }
                    1 -> {
                        houseBean.gLoan = gLoan.toDouble()
                        houseBean.gRate = gRate.toDouble()
                    }
                    2 -> {
                        houseBean.sLoan = sLoan.toDouble()
                        houseBean.sRate = sRate.toDouble()

                        houseBean.gLoan = gLoan.toDouble()
                        houseBean.gRate = gRate.toDouble()
                    }
                }
                val bundle = Bundle()
                bundle.putSerializable("time", houseBean)
                intent.putExtras(bundle)
                startActivity(intent)
            }
        }


        house_s_interest_image.setOnClickListener {
            showDefineDialog("15年10月24日商业贷款基准利率为:4.9%")
        }

        house_g_interest_image.setOnClickListener {
            showDefineDialog("15年10月24日公积金贷款基准利率为:3.25%")
        }

        house_s_time_image.setOnClickListener {
            showDefineDialog("目前最高还款期限为30年(360期)")
        }
    }

    /**
     * 显示对话框
     */
    private fun showDefineDialog(value: String) {
        val builder = AlertDialog.Builder(this);
        builder.setTitle("提示")
        builder.setMessage(value)
        builder.setPositiveButton("确定") { _, _ ->

        }
        builder.show()
    }


    /**
     *  数据检测
     */
    private fun checkData(): Boolean {
        val total = house_total_edit.text.toString()
        if (TextUtils.isEmpty(total)) {
            T.showShort(this, "请输入房子总价")
            return false
        }

        val sLoan = house_s_menoy_edit.text.toString()
        val sRate = house_s_interest_edit.text.toString()


        val gLoan = house_g_menoy_edit.text.toString()
        val gRate = house_g_interest_edit.text.toString()

        when (tabPosition) {
            0 -> {
                if (TextUtils.isEmpty(sRate)) {
                    T.showShort(this, "请输入商业贷款利率")
                    return false
                }
            }
            1 -> {
                if (TextUtils.isEmpty(gRate)) {
                    T.showShort(this, "请输入公积金贷款利率")
                    return false
                }
            }
            2 -> {
                if (TextUtils.isEmpty(sLoan)) {
                    T.showShort(this, "请输入商业贷款金额")
                    return false
                }

                if (TextUtils.isEmpty(gLoan)) {
                    T.showShort(this, "请输入公积金贷款金额")
                    return false
                }

                if ((sLoan.toDouble() + gLoan.toDouble()) > (Utils.keepPointDouble(total.toDouble() * poor))) {
                    T.showShort(this, "您的商业贷款+公积金贷款总和超过了按揭总额了")
                    return false
                }
            }
        }
        val data = house_s_time_edit.text.toString()
        if (TextUtils.isEmpty(data)) {
            T.showShort(this, "请输入贷款年限")
            return false
        }

        if (data.toInt() < 1) {
            T.showShort(this, "贷款年限必须大于等于1")
            return false
        }

        return true
    }


    public override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }
}
